

/**
 * Created by Administrator on 2016/11/7.
 */
var _interface = require('../../models/interface/interface');
const logger   = require('../../../common/logger').logger;
var fs = require('fs');

// 最近三十天与今年 格式：new Date() to 2012-03-21
function dateToGs(type) {
  var d = new Date();
  // 当前日期
  var month = d.getMonth() + 1 > 9 ? (d.getMonth() + 1) : '0' + (d.getMonth() + 1);
  var day = d.getDate() > 9 ? d.getDate() : '0' + d.getDate();
  var endDate = d.getFullYear() + '-' + month + '-' + day;

  // 三十天前日期
  var bDi = d.setDate(d.getDate() - 30);
  var bD = new Date(bDi);
  var bMonth = bD.getMonth() + 1 > 9 ? (bD.getMonth() + 1) : '0' + (bD.getMonth() + 1);
  var bDay = bD.getDate() > 9 ? bD.getDate() : '0' + bD.getDate();
  var beginDate = bD.getFullYear() + '-' + bMonth + '-' + bDay;

  if (type == 'month') {
    var time = {
      beginDate: beginDate,
      endDate: endDate
    }
  }

  if (type == 'year') {
    var time = {
      beginDate: d.getFullYear() + '-01-01',
      endDate: endDate
    }
  }

  if (type == '') {
    var time = {
      beginDate: '',
      endDate: ''
    }
  }

  return time;
}

exports.getImage = function(req, res, next) {
  _interface.getImage({
    fileName: req.query.fileName || '',
  })
  .withCookie( req.headers.cookie )
  .done(function(data) {
    var buffer = new Buffer(data)
    res.writeHead(200,{"Content-Type":'image/jpeg'});
    res.end(buffer.toString(),"binary")
  });
}

// 获取业务系统
exports.ssoServiceSystemList = function(req, res, next) {
  _interface.ssoServiceSystemList({})
  .withCookie( req.headers.cookie )
  .done(data => {
    res.send(JSON.stringify(data));
  })
}
// 删除业务系统
exports.deleteSystem = function(req, res, next) {
  _interface.deleteSystem({
    systemCode: req.body.systemCode
  })
  .withCookie( req.headers.cookie )
  .done(data => {
    res.send(JSON.parse(data));
  })
}

// 小百科
exports.kcloudAccountList = function(req, res, next) {
  _interface.kcloudAccountList({
    userName: req.body.userName || '',
    page: req.body.page || 1,
    size: req.body.size || 10
  })
  .withCookie( req.headers.cookie )
  .done(function(data) {
    res.send(JSON.parse(data));
  });
};

// 删除账户
exports.deleteAccount = function(req, res, next) {
  _interface.deleteAccount({
    id: req.body.id || '',
  })
  .withCookie( req.headers.cookie )
  .done(function(data) {
    res.send(JSON.parse(data));
  });
};

// 搜索服务次数统计
exports.addAccount = function(req, res, next) {
  _interface.addAccount({
    "contactsPhone": req.body.contactsPhone || '',
    "contacts": req.body.contacts || '',
    "bussinessSystem": req.body.bussinessSystem || '',
    "customer": req.body.customer || '',
    "userName": req.body.userName || '',
  })
  .withCookie( req.headers.cookie )
  .done(function(data) {
    res.send(JSON.parse(data));
  });
};

exports.updateAccount = function(req, res, next) {
  _interface.updateAccount({
    "id": req.body.id || '',
    "contactsPhone": req.body.contactsPhone || '',
    "contacts": req.body.contacts || '',
    "bussinessSystem": req.body.bussinessSystem || '',
    "customer": req.body.customer || '',
    "userName": req.body.userName || '',
  })
  .withCookie( req.headers.cookie )
  .done(function(data) {
    res.send(JSON.parse(data));
  });
};

// 组件-模块查询
exports.qeuryModules = function(req, res, next) {
  _interface.qeuryModules({})
  .withCookie( req.headers.cookie )
  .done(data => {
    res.send(JSON.parse(data));
  });
}

// 组件-查询模块下的所有组件
exports.getComponentList = function(req, res, next) {
  _interface.getComponentList({
    moduleCode: req.body.moduleCode
  })
  .withCookie( req.headers.cookie )
  .done(data => {
    res.send(JSON.parse(data));
  });
}

// 组件-查询组件信息
exports.select = function(req, res, next) {
  logger.info(req.body);
  _interface.select({
    name: req.body.name,
    componentCode: req.body.componentCode,
  })
  .withCookie( req.headers.cookie )
  .done(data => {
    res.send(JSON.parse(data));
  });
}

// 组件-删除文件
exports.deleteFile = function(req, res, next) {
  _interface.deleteFile({
    fileName: req.body.fileName,
  })
  .withCookie( req.headers.cookie )
  .done(data => {
    res.send(JSON.parse(data));
  });
}

// 组件-更新
exports.componentUpdate = function(req, res, next) {
  var data = {
    name: req.body.name,
    remark: req.body.remark,
    componentCode: req.body.componentCode,
    type: req.body.type,
    moduleCode: req.body.moduleCode,
    cocurrencyCount: req.body.cocurrencyCount,
    responseDuration: req.body.responseDuration,
    componentDescription: req.body.componentDescription,
    apiUrl: req.body.apiUrl,
    status: req.body.status,
    fileName: req.body.fileName
  }
  if (req.body.addOrUpdate) {
    _interface.componentAdd(data)
    .withCookie( req.headers.cookie )
    .done(data => {
      res.send(JSON.parse(data));
    });
  } else {
    _interface.componentUpdate(data)
    .withCookie( req.headers.cookie )
    .done(data => {
      res.send(JSON.parse(data));
    });
  }
}

// 组件-更新
exports.updateComponentCode = function(req, res, next) {
  _interface.updateComponentCode({
    newCode: req.body.newCode,
    oldCode: req.body.oldCode
  })
  .withCookie( req.headers.cookie )
  .done(data => {
    res.send(JSON.parse(data));
  });
}

// 组件-新增模块
exports.addModule = function(req, res, next) {
  _interface.addModule({
    moduleName: req.body.moduleName
  })
  .withCookie( req.headers.cookie )
  .done(data => {
    res.send(JSON.parse(data));
  });
}

// 业务系统-新增
exports.addServiceSystem = function(req, res, next) {
  _interface.addServiceSystem({
    name: req.body.name,
    systemImage: req.body.systemImage,
    remark: req.body.remark
  })
  .withCookie( req.headers.cookie )
  .done(data => {
    res.send(JSON.parse(data));
  });
}

// 登录
exports.login = function(req, res, next) {
  _interface.login({
    userName: req.body.userName,
    password: req.body.password,
    systemCode: req.body.systemCode
  })
  .withCookie( req.headers.cookie )
  .done((userData, setCookies) => {
    logger.info(res.headers)
    var data = JSON.parse(userData);
    if (data.code === '0') {
      req.session.userInfo = data.data;
      res.setHeader( 'Set-Cookie', setCookies );
    }
    res.send(data);
  });
}

// 登出
exports.loginOut = function(req, res, next) {
  _interface.loginOut({})
  .withCookie( req.headers.cookie )
  .done(data => {
    delete req.session.userInfo;
    delete req.session.info;
    res.send(JSON.parse(data));
  });
}


// 业务系统背景图片上传
exports.uploadPicture = function(req, res, next) {
      console.info(req)
      var url = req.url;

      var obj = req.files.file;
      var tmp_path = obj.path;
      var new_path = "public/upload";

      /*修改上传文件地址*/
      upload(req,res,function(err){
        if (err) {
            console.log('上传失败');
        }else{
            console.log('上传成功');
        }
    });

    // 反馈上传信息
     res.send({
          'states':'success'
     });

}



